package com.innovation.chengzhangriji.ui;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.innovation.chengzhangriji.ChZhRJApplication;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.http.Client;

public class BaseFragment extends Fragment {

    private Toast tipError;

    private NetworkUnavailableListenerImpl mNetworkUnavailableListenerImpl;

    private ProgressDialog progress;

    protected View contentView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tipError = Toast.makeText(getActivity(), "", Toast.LENGTH_SHORT);
        mNetworkUnavailableListenerImpl = new NetworkUnavailableListenerImpl();
    }

    public void onResume() {
        super.onResume();
        onPageStart();
    }

    public void onPause() {
        super.onPause();
        onPageEnd();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(contentView != null) {
            ((ViewGroup)contentView.getParent()).removeView(contentView);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        ChZhRJApplication.getRefWatcher(getActivity()).watch(this);
    }

    /**
     * <h3>页面是使用FragmentActivity + Fragment实现的,在此处调用以下方法</h3>
     * <p>MobclickAgent.onPageStart(String);</p>
     */
    protected void onPageStart() {

    }

    /**
     * <h3>页面是使用FragmentActivity + Fragment实现的,在此处调用以下方法</h3>
     * <p>MobclickAgent.onPageEnd(String);</p>
     */
    protected void onPageEnd() {

    }

    protected final Toast tipShow(String value) {
        tipError.setText(value);
        tipError.show();
        return tipError;
    }

    protected final Toast tipShow(int resId) {
        tipError.setText(resId);
        tipError.show();
        return tipError;
    }

    protected final Client.NetworkUnavailableListener networkUnavailableSampleImpl() {
        return mNetworkUnavailableListenerImpl;
    }

    protected final void showProgressDialog(String title, String message) {
        progress = ProgressDialog.show(getActivity(), title, message, true);
    }

    protected final void dismissProgressDialog() {
        progress.dismiss();
    }

    private class NetworkUnavailableListenerImpl implements Client.NetworkUnavailableListener {

        @Override
        public void networkUnavailable() {
            tipShow(R.string.tip_network_unavailable);
        }
    }
}
